from goods_info.models import UnitsModel
from rest_framework import serializers


class UnitsSerializer(serializers.ModelSerializer):
    """
    计量单位的序列化器
    """
    # 指定新增的属性，是从一个函数中得到值
    units_name = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = UnitsModel
        fields = ['id', 'basic_name', 'backup_name', 'delete_flag', 'units_name']

    def get_units_name(self, obj):
        return str(obj)  # 就是调用该对象的__str__函数
